<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2019/09/03
 * Time: 14:58
 */

namespace business\datacenter\repositories;

use business\company\models\SupplierBasicModel;
use business\datacenter\models\PlatformDataRsyncToEsCronRecordModel;
use business\datacenter\models\PlatformDataRsyncToEsErrorLogModel;
use yii\db\Query;

class SupplierBasicRepository
{
    public function getSupplierBasicOne
    (
        $condition = [],
        $fields = '*',
        $orderBy = ['created_time' => SORT_DESC]
    ) {
        return (new Query())
            ->from(SupplierBasicModel::tableName())
            ->select($fields)
            ->where($condition)
            ->orderBy($orderBy)
            ->createCommand(SupplierBasicModel::getDb())
            ->queryOne();
    }

    public function getSupplierBasicList
    (
        $condition = [],
        $starTime = 0,
        $endTime = 0,
        $fields = '*',
        $offset = 0,
        $pageSize = 15,
        $orderBy = ['last_updated_time' => SORT_DESC]
    ) {
        $query = (new Query())
            ->from(SupplierBasicModel::tableName())
            ->select($fields)
            ->where($condition);

        (!empty($starTime)) && $query->andWhere(['>=', 'last_updated_time', $starTime]);
        (!empty($endTime))  && $query->andWhere(['<=', 'last_updated_time', $endTime]);

        return $query->offset($offset)
            ->limit($pageSize)
            ->orderBy($orderBy)
            ->createCommand(SupplierBasicModel::getDb())
            ->queryAll();
    }

    /**
     * 返回平台供应商数据全量/增量同步到es调度执行记录
     * @param array $condition
     * @param string $fields
     * @param array $orderBy
     * @return mixed
     */
    public function getPlatformDataRsyncToEsCronRecord(
        $condition = [],
        $fields = '*',
        $orderBy =
        ['created_time' => SORT_DESC]
    ) {
        return (new Query())
            ->from(PlatformDataRsyncToEsCronRecordModel::tableName())
            ->select($fields)
            ->where($condition)
            ->orderBy($orderBy)
            ->createCommand(PlatformDataRsyncToEsCronRecordModel::getDb())
            ->queryOne();
    }

    /**
     * 插入平台供应商数据同步到es调度执行记录表
     * @param array $data
     * @return array|int
     * @throws \yii\db\Exception
     */
    public function storePlatformDataRsyncToEsCronRecord($data = [])
    {
        if (!empty($data)) {
            return (new Query())
                ->createCommand(PlatformDataRsyncToEsCronRecordModel::getDb())
                ->insert(PlatformDataRsyncToEsCronRecordModel::tableName(), $data)
                ->execute();
        }

        return [];
    }

    /**
     * 插入平台供应商数据全量/增量同步到es中错误日志记录
     * @param array $data
     * @return array|int
     * @throws \yii\db\Exception
     */
    public function storePlatformDataRsyncToEsErrorLog($data = [])
    {
        if (!empty($data)) {
            return (new Query())
                ->createCommand(PlatformDataRsyncToEsErrorLogModel::getDb())
                ->insert(PlatformDataRsyncToEsErrorLogModel::tableName(), $data)
                ->execute();
        }

        return [];
    }
}
